package com.java1412.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.java1412.entity.PayInfo;
import com.java1412.entity.User;
import com.java1412.mapper.PayInfoMapper;
import com.java1412.mapper.UserMapper;
import com.java1412.service.PayInfoService;
import com.java1412.utils.PageUtil;
import com.java1412.utils.ResponseUtil;
import com.java1412.vo.PayInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.beans.Beans;
import java.util.*;

/**
 * @author java1412
 * @since 2023-08-11 14:43:30
 */
@Service
@Transactional
public class PayInfoServiceImpl implements PayInfoService {

    @Resource
    private PayInfoMapper payInfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil add(PayInfo payInfo) {
        payInfo.setPayPlatform(1); // 1表示支付宝
        payInfo.setCreateTime(new Date());
        payInfo.setUpdateTime(new Date());
        return payInfoMapper.add(payInfo).intValue() > 0 ?
                ResponseUtil.ok("添加支付信息成功") : ResponseUtil.fail("添加支付信息失败");
    }

    @Override
    public ResponseUtil find(Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap();
        Integer startIndexNo = (currentPage - 1) * pageSize;
        map.put("startIndexNo", startIndexNo);
        map.put("pageSize", pageSize);
        Integer total = payInfoMapper.findCount(map);
        List<PayInfo> payInfoList = payInfoMapper.find(map);
        List<PayInfoVo> payInfoVoList = new ArrayList<>();
        for (PayInfo payInfo : payInfoList) {
            PayInfoVo payInfoVo = new PayInfoVo();
            BeanUtils.copyProperties(payInfo, payInfoVo);
            User user = userMapper.findById(payInfo.getUserId()); // 根据id查询用户信息
            payInfoVo.setUser(user);
            payInfoVoList.add(payInfoVo);
        }
        PageUtil<PayInfoVo> pageUtil = new PageUtil<>(currentPage, pageSize, total, payInfoVoList);
        return ResponseUtil.ok("查询支付信息成功", pageUtil);
    }
}
